﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TimKiem
{
    public partial class frmTimKiemHangHoaTheoHoaDonBanBuon : Form
    {
        public frmTimKiemHangHoaTheoHoaDonBanBuon()
        {
            InitializeComponent();
        }
        private string maHoaDon;

        public string MaHoaDon
        {
            get { return maHoaDon; }
            set { maHoaDon = value; }
        }

        public frmTimKiemHangHoaTheoHoaDonBanBuon(string maHoaDon)
        {
            InitializeComponent();
            this.maHoaDon = maHoaDon;
        }

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        Common.StaticVariable.nhacungcap = null;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvHienThi.Rows.Count <= 0)
            { return; }
            MaHangHoa = dgvHienThi.CurrentRow.Cells["MaHangHoa"].Value.ToString();
            this.GetRow();
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            this.Chon();
        }
        private void Chon()
        {
            try
            {
                if (this.banghi == null)
                {
                    MessageBox.Show("Bạn phải chọn hàng hóa.", "Thông báo");
                    return;
                }
                Common.StaticVariable.timhanghoatheohdbanbuon = banghi;
                this.Close();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        private string MaHangHoa = string.Empty;
        private Entities.HienThiHangHoaListview banghi = null;
        private void GetRow()
        {
            try
            {
                if (hanghoa == null) { return; }
                foreach (var item in hanghoa.ToArray())
                {
                    if (item.MaHangHoa == this.MaHangHoa)
                    {
                        banghi = item;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        Entities.HienThiHangHoaListview[] hanghoa = null;
        private void Select()
        {
            try
            {
                Entities.HienThiHangHoaListview r = new Entities.HienThiHangHoaListview();
                r.HanhDong = "6";
                r.MaKho = "";
                r.MaNhaCungCap = "";
                r.MaHoaDon = this.maHoaDon;
                hanghoa = Common.StaticVariable.InterfaceObject.SelectChiTietHangHoaCacBang(r);
                if (hanghoa== null)
                {
                    this.Gridview();
                    return;
                }
                dgvHienThi.DataSource = hanghoa;
                this.Gridview();
            }
            catch (Exception)
            {
            }
        }
        private void Gridview()
        {
            try
            {
                if (hanghoa == null)
                {
                    Entities.HienThiHangHoaListview[] nv = new Entities.HienThiHangHoaListview[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["MaHangHoa"].Visible = true;
                dgvHienThi.Columns["MaHangHoa"].HeaderText = "Mã hàng hóa";
                dgvHienThi.Columns["TenHangHoa"].Visible = true;
                dgvHienThi.Columns["TenHangHoa"].HeaderText = "Tên hàng hóa";
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void frmTimKiemHangHoaTheoHoaDonBanBuon_Load(object sender, EventArgs e)
        {
            this.Select();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            this.Chon();
        }
    }
}
